﻿using System.IO;
using HardMock;
using Xunit;

namespace AcceptanceTests
{
    public class ByRefParameterFeature
    {
        // ReSharper disable MemberCanBePrivate.Global
        public interface IOutParameter
        // ReSharper restore MemberCanBePrivate.Global
        {
            void SetParameter(out string value);
        }

        class OutParameter : IOutParameter
        {
            public void SetParameter(out string value)
            {
                value = "hello";
            }
        }

        [Fact]
        public void Should_record_out_parameter_from_method_call()
        {
            var recorder = new OutParameter().AsRecorder<IOutParameter>();

            string actual;

            recorder.SetParameter(out actual);

            Assert.Equal("hello", actual);

            using (var stream = new MemoryStream())
            {
                recorder.Snapshot(stream);
                string actualReplay;

                stream.BuildMockFrom<IOutParameter>().SetParameter(out actualReplay);
                Assert.Equal("hello", actualReplay);
            }
        }

        [Fact]
        public void Should_replay_for_by_ref_args_when_args_value_changed()
        {
            var recorder = new MUODate().AsRecorder<_MUODate>();

            var b = "pouet";
            recorder.ControlByRefParam(ref b);
            Assert.Equal("toto", b);

            using (var stream = new MemoryStream())
            {
                recorder.Snapshot(stream);

                var replayer = stream.BuildMockFrom<_MUODate>();
                var p = "pouet";
                replayer.ControlByRefParam(ref p);
                Assert.Equal("toto", p);
            }
        }
    }
}
